<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下单时间" prop="orderTime">
        <el-date-picker clearable
                        v-model="queryParams.orderTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择下单时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['orders:orders:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ordersList">
      <el-table-column label="订单号" align="center" prop="number"/>
      <el-table-column label="订单状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status==1">待付款</el-tag>
          <el-tag v-else-if="scope.row.status==2">待派送</el-tag>
          <el-tag v-else-if="scope.row.status==3">已派送</el-tag>
          <el-tag v-else-if="scope.row.status==4">已完成</el-tag>
          <el-tag v-else>已取消</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="下单时间" align="center" prop="orderTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实收金额" align="center" prop="amount"/>
      <el-table-column label="手机" align="center" prop="phone"/>
      <el-table-column label="地址" align="center" prop="address"/>
      <el-table-column label="用户名" align="center" prop="userName"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleViewOrder(scope.row)"
          >查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="updateCancel(scope.row)"
            v-hasPermi="['system:orders:update']"
            v-if="scope.row.status==1"
          >取消
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="updateSend(scope.row)"
            v-hasPermi="['system:orders:update']"
            v-if="scope.row.status==2"
          >派送
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="updateComplete(scope.row)"
            v-hasPermi="['system:orders:update']"
            v-if="scope.row.status==3"
          >完成
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog
      title="订单信息"
      :visible.sync="open"
      width="40">
      <div>
        <table>
          <tr>
            <td>订单号:</td>
            <td><el-tag>{{this.ViewOrder.number}}</el-tag></td>
            <td>订单状态:</td>
            <td>
              <el-tag v-if="this.ViewOrder.status==1">待付款</el-tag>
              <el-tag v-else-if="this.ViewOrder.status==2">待派送</el-tag>
              <el-tag v-else-if="this.ViewOrder.status==3">已派送</el-tag>
              <el-tag v-else-if="this.ViewOrder.status==4">已完成</el-tag>
              <el-tag v-else>已取消</el-tag>
            </td>
          </tr>
          <tr>
            <td>收货人:</td>
            <td><el-tag>{{this.ViewOrder.consignee}}</el-tag></td>
            <td>联系电话:</td>
            <td><el-tag>{{this.ViewOrder.phone}}</el-tag></td>
          </tr>
          <tr>
            <td>地址:</td>
            <td><el-tag>{{this.ViewOrder.address}}</el-tag></td>
            <td>支付金额:</td>
            <td><el-tag>￥{{this.ViewOrder.amount}}</el-tag></td>
          </tr>
          <tr>
            <td>下单时间:</td>
            <td><el-tag>{{ parseTime(this.ViewOrder.orderTime, '{y}-{m}-{d}') }}</el-tag></td>
          </tr>
        </table>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="open = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {listOrders, getOrders, delOrders, addOrders, updateOrders} from "@/api/system/orders";

export default {
  name: "Orders",
  data() {
    return {
      //显示订单
      ViewOrder:{
        number:0,
        status:0,
        consignee:'',
        phone:'',
        address:'',
        amount:0,
        order_time:''
      },
      updateOrders:{
        status: 0,
        id:0
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      ordersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        number: null,
        orderTime: null,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    updateComplete(data){
      this.updateOrders.status=4
      this.updateOrders.id=data.id;
      updateOrders(this.updateOrders);
      this.getList();
      this.$modal.msgSuccess("修改成功");
    },
    updateSend(data){
      this.updateOrders.status=3;
      this.updateOrders.id=data.id;
      updateOrders(this.updateOrders)
      this.getList();
      this.$modal.msgSuccess("修改成功");
    },
    updateCancel(data){
      this.updateOrders.status=5;
      this.updateOrders.id=data.id;
      updateOrders(this.updateOrders)
      this.getList();
      this.$modal.msgSuccess("修改成功");
    },
    /** 查看订单列表 */
    handleViewOrder(data) {
      Object.assign(this.ViewOrder,data);
      this.open = true;
    },
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listOrders(this.queryParams).then(response => {
        this.ordersList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单编号为"' + ids + '"的数据项？').then(function () {
        return delOrders(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('orders/orders/export', {
        ...this.queryParams
      }, `orders_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
